<template>
  <div id="shop_div">
    <el-table :data="shops">
      <el-table-column prop="shopNumber" label="编号"></el-table-column>
      <el-table-column prop="shopName" label="商品名称"></el-table-column>
      <el-table-column prop="shopPrice" label="商品价格"></el-table-column>
      <el-table-column prop="type.name" label="商品类型"></el-table-column>
      <el-table-column prop="shopUnit" label="商品规格"></el-table-column>
      <el-table-column label="操作" width="300">
        <template slot="header" slot-scope="scope">
          <el-row>
            <el-col :span="22" :offset="2">
              <el-input
                v-model="shop.shopName"
                size="medium"
                @keyup.enter.native="search"
                placeholder="输入商品名搜索">
                <el-button slot="append" @click="search" icon="el-icon-search"></el-button>
              </el-input>
            </el-col>
          </el-row>
        </template>
        <template slot-scope="scope">
          <el-button size="mini" v-if="scope.row.isUp==0" type="warning" @click="isUp(scope.row.id,1)">
            上架
          </el-button>
          <el-button size="mini" v-if="scope.row.isUp==1" type="info" @click="isUp(scope.row.id,0) ">
            下架
          </el-button>
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="shopEdit(scope.row.id)">
            编辑
          </el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="shopDel(scope.row.id)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <div>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageno"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
    <div>
      <el-dialog
        title="编辑"
        :visible.sync="dialogVisible"
        width="50%"
        :before-close="handleClose"
      >
        <component ref="editShop" is="EditShop"></component>
      </el-dialog>
      <up-shop-num ref="upShopNum"></up-shop-num>
    </div>
  </div>
</template>

<script>
  import EditShop from "./EditShop";
  import UpShopNum from "./UpShopNum";

  export default {
    name: "ShopView",
    data() {
      return {
        shops: [],
        shop: {id: "", shopNumber: "", shopName: "", shopPrice: "", shopAddr: "", isUp: ""},
        pageno: 1,
        pagesize: 5,
        total: 1,
        dialogVisible: false
      }
    },
    methods: {
      getMsg(code, msg) {
        //提示信息
        if (code > 0) {
          this.$message({
            type: "success",
            message: msg,
          });
          this.getShopData();
        } else {
          this.$message({
            type: "error",
            message: msg,
          });
        }
      },
      getShopData() {
        //组装数据
        var params = new URLSearchParams();
        params.append("pageno", this.pageno);
        params.append("pagesize", this.pagesize);
        Object.keys(this.shop).forEach(item => {
          params.append(item, this.shop[item]);
        })
        this.$axios.get("shop/shop.action", {params: params})
          .then(res => {
            this.shops = res.data.records;
            this.total = res.data.total;
          })
          .catch(err => {
            alert(err);
          })
      },
      search() {//按商品名称搜索
        this.getShopData();
      },
      handleSizeChange(val) {
        //每页范围
        this.pagesize = val;
        this.getShopData();
      },
      handleCurrentChange(val) {
        //当前页
        this.pageno = val;
        this.getShopData();
      },
      shopEdit(id) {
        this.dialogVisible = true;
        //根据id查询商品 并打开商品编辑
        this.$nextTick(item => {
          this.$refs.editShop.getData(id);
        })
      },
      handleClose(done) {
        //模态框的关闭
        this.$confirm("确认关闭？")
          .then((_) => {
            this.$refs.editShop.resetForm();
          })
          .catch((_) => {
          });
      },
      shopDel(id) {
        //删除商品
        let params={};
        params['id']=id;
        this.$axios
          .delete("shop/deleteShopById.action",{params:params})
          .then(res=>{
            if (res.data.code>0){
              this.$message.success(res.data.msg);
              this.getShopData();
            }else {
              this.$message.error(re.data.msg);
            }
          })
          .catch(err => {
            this.$message.error(err)
          })
      },
      isUp(id, state) {
        //上架
        if (state == 1) {
          this.$refs.upShopNum.shop.id = id;
          this.$refs.upShopNum.shop.isUp = state;
          this.$refs.upShopNum.dialogVisible = true;
          this.$nextTick(item => {
            this.$refs.upShopNum.getUpNum()
          })
        } else {
          //下架
          var shop = {id: id, isUp: state}
          this.$axios
            .put("shop/shop.action", shop)
            .then(res => {
              this.getMsg(res.data.code, res.data.msg)
            })
            .catch(err => alert(err))
        }

      },

    },
    components: {
      EditShop, UpShopNum
    },
    created() {
      this.getShopData();
    }
  }
</script>

<style scoped>

</style>
